<template>
  <el-dialog title="分配角色" :visible="showRoleDialog" @close="btnCancle">
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-button @click="btnOk">确定</el-button>
      <el-button @click="btnCancle">取消</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    userId: {
      type: String,
      default: null
    //   reuired: true
    }
  },
  data() {
    return {
      roleIds: [], // 当前用户所拥有的角色id
      roleList: []// 负责当前所有角色的id
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const { rows } = await getRoleList({ page: 1, pagesize: 20 })// 默认取20条
      this.roleList = rows
      console.log(rows)
    },
    // 这个方法什么时候调用?props的传值是异步的所以这里不能用this.userId
    // 这个方法是给父组件调用的,父组件调用子组件的方法通过ref
    async getUserDetailById(id) {
      const { roleIds } = await getUserDetailById(id) // 将当前用户所拥有的角色赋值给当前用户对象
      this.roleIds = roleIds
    },
    async btnOk() {
      await assignRoles({ id: this.userId, roleIds: this.roleIds })
      // 关闭弹层
      this.$emit('update:showRoleDialog', false)
    },
    btnCancle() {
      this.roleIds = []
      this.$emit('update:showRoleDialog', false)
    }
  }
}
</script>

<style>

</style>
